﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// fuction 的摘要说明
/// </summary>
public class fuction
{
	public fuction()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public static bool CheckUidFilter(string Input)
    {
        Input = Input.ToLower();
        string strFilter = "',%27,;,%3B,//,--,exec,declare,update,insert,delete, and ,nchar,select,hanzari,sysobjects,customerinfo";
        string[] str = strFilter.Split(',');
        for (int i = 0; i < str.Length; i++)
        {
            if (Input.IndexOf(str[i]) > -1)
                return false;

        }
        return true;
    }
    public static bool CheckUidAcc(string Input)
    {
        Byte[] MyBytes = System.Text.Encoding.ASCII.GetBytes(Input);
        for (int i = 0; i < Input.Length; i++)
        {
            if (i == 0)
            {
                if (MyBytes[i] < 97 || MyBytes[i] > 122)
                    return false;
            }
            else
            {
                if (MyBytes[i] < 48 || MyBytes[i] > 122)
                    return false;
                else if ((MyBytes[i] >= 58 && MyBytes[i] <= 64) || (MyBytes[i] >= 91 && MyBytes[i] <= 96))
                    return false;
            }
        }

        return true;
    }

    public static string CleanRequest(string strInput)
    {
        strInput = strInput.ToLower();//转换为小写.

        strInput = strInput.Replace("'", "");
        strInput = strInput.Replace("update", "");
        strInput = strInput.Replace("execute", "");
        strInput = strInput.Replace("delete", "");
        strInput = strInput.Replace(";", "");
        strInput = strInput.Replace("[", "");
        strInput = strInput.Replace("]", "");
        strInput = strInput.Replace("{", "");
        strInput = strInput.Replace("}", "");
        strInput = strInput.Replace("<", "");
        strInput = strInput.Replace(">", "");
        strInput = strInput.Replace(".", "");
        strInput = strInput.Replace("%", "");
        strInput = strInput.Replace(",", "");
        strInput = strInput.Replace("@", "");
        strInput = strInput.Replace("&", "");
        strInput = strInput.Replace("/", "");

        strInput = strInput.Replace("+", "");
        strInput = strInput.Replace("-", "");
        strInput = strInput.Replace("(", "");
        strInput = strInput.Replace(")", "");
        strInput = strInput.Replace("=", "");
        strInput = strInput.Replace("select", "");
        strInput = strInput.Replace("script", "");
        strInput = strInput.Replace("object", "");
        strInput = strInput.Replace("applet", "");

        strInput = strInput.Replace("exec", "");
        strInput = strInput.Replace("join", "");
        strInput = strInput.Replace("union", "");
        strInput = strInput.Replace("where", "");
        strInput = strInput.Replace("insert", "");


        strInput = strInput.Replace("like", "");
        strInput = strInput.Replace("drop", "");
        strInput = strInput.Replace("create", "");
        strInput = strInput.Replace("rename", "");
        strInput = strInput.Replace("count", "");
        strInput = strInput.Replace("chr", "");
        strInput = strInput.Replace("truncate", "");
        strInput = strInput.Replace("mid", "");
        strInput = strInput.Replace("create", "");
        strInput = strInput.Replace("mid", "");
        strInput = strInput.Replace("nchar", "");
        strInput = strInput.Replace("char", "");
        strInput = strInput.Replace("alter", "");
        strInput = strInput.Replace("cast", "");
        strInput = strInput.Replace("exists", "");
        return strInput;
    }

    #region 常规验证
    /// <summary>
    /// 是否是数字
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsNumber(string value)
    {
        if (value == null)
            return false;
        value = value.Trim();
        Regex re = new Regex("^[+\\-]{0,1}\\d+$", RegexOptions.Compiled);
        return re.IsMatch(value.Trim());
    }

    /// <summary>
    /// 是否是合法字符串(空或空格为不合法)
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsString(string value)
    {
        if (value == null)
            return false;
        value = value.Trim();
        return value == "" ? false : true;
    }

    /// <summary>
    /// 过滤sql非法字符
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string FilterString(string value)
    {
        value = HttpContext.Current.Server.HtmlDecode(value).ToLower();
        Regex re1 = new Regex("\\s+select\\s+", RegexOptions.Compiled);
        re1.Replace(value, "");
        Regex re2 = new Regex("\\s+or\\s+", RegexOptions.Compiled);
        re2.Replace(value, "");
        Regex re3 = new Regex("\\s+and\\s+", RegexOptions.Compiled);
        re3.Replace(value, "");
        Regex re4 = new Regex("\\s+update\\s+", RegexOptions.Compiled);
        re4.Replace(value, "");
        Regex re5 = new Regex("\\s+delete\\s+", RegexOptions.Compiled);
        re5.Replace(value, "");
        Regex re6 = new Regex("\\s+insert\\s+", RegexOptions.Compiled);
        re6.Replace(value, "");


        value = value.Replace(",", "");
        value = value.Replace("'", "");

        return value;
    }


    #endregion

    #region 用户验证
    /// <summary>
    /// 验证用户名字符串是否合法
    /// </summary>
    /// <param name="uid"></param>
    /// <returns></returns>
    public static bool CheckUidBySTR(string uid)
    {
        Regex re = new Regex(@"^[a-z]([0-9]|[a-z]){4,14}$");
        return re.IsMatch(uid);
    }

    public static bool CheckUidBySTR_m(string uid)
    {
        Regex re = new Regex(@"^[a-z]([0-9]|[a-z]|@|.){4,19}$");
        return re.IsMatch(uid);
    }
    /// <summary>
    /// 验证昵称是否合法
    /// </summary>
    /// <param name="nick"></param>
    /// <returns></returns>
    public static bool CheckNick(string nick)
    {
        string[] keys = { "我操", "我日你妈", "MM", "JJ", "BB", "脱", "@", ".com", "拖", "托", "操你妈", "你好难看呀", "我想操你", "我想干你", "你中奖了", "系统消息", "JB", "SB", "几吧", "煞笔", "www.", "http://", "baobei", "admin", "&", "%", "'", " and ", " or " };
        foreach (string i in keys)
        {
            if (nick.IndexOf(i) > -1)
                return false;
        }
        return true;

    }

    #endregion

    #region
    /// <summary>
    /// 数字签名验证
    /// </summary>
    /// <param name="values"></param>
    /// <param name="signKey"></param>
    /// <param name="sign"></param>
    /// <returns></returns>
    public static bool SignCheckout(string[] values, string signKey, string sign)
    {
        string signStr = "";
        for (int i = 0; i < values.Length; i++)
        {
            signStr += values[i];
        }
        signStr += signKey;
        string sign_md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(signStr, "MD5").ToLower();

        return sign_md5 == sign;
    }
    #endregion
}
